<template>
    <div>
        <div class="header-info">
            <p>{{action_name}}-站点</p>
            <div v-if="jv.main.user.isAdmin">
                <el-button size="mini" @click="$router.push('/site/b-site-info/add')" v-if="action=='edit'">新建
                </el-button>
                <el-button size="mini" @click="save_click" type="primary"> 保存</el-button>
            </div>
        </div>
        <el-row :gutter="12" type="flex" ref="card">
            <el-col>
                <el-card shadow="always">
                    <div slot="header">
                        <span>基本信息</span>
                    </div>
                    <kv label="名称">
                        <el-input v-model="info.name" chk="*"/>
                    </kv>
                    <kv label="appId" v-if="id">
                        <div v-html="info.id"></div>
                    </kv>
                    <kv label="秘钥" v-if="id">
                        <div>
                            <div v-html="info.secret">
                            </div>
                            <el-button size="mini" @click="gen_secret()" v-if="jv.main.user.isAdmin">重新生成</el-button>
                        </div>
                    </kv>

                    <kv label="有效开始时间">
                        <el-input v-model="info.startAt" placeholder="选择开始时间"
                                  chk=": if(!data.info || !value) return ; if( (/^([01]?\d|2[0-3]):[0-5]?\d:[0-5]?\d$/).test(value) == false) return '非法时间格式';">
                            <template slot="append">时:分:秒</template>
                        </el-input>
                    </kv>
                    <kv label="有效结束时间">
                        <el-input v-model="info.endAt" placeholder="选择结束时间"
                                  chk=": if(!data.info || !value) return ; if( (/^([01]?\d|2[0-3]):[0-5]?\d:[0-5]?\d$/).test(value) == false) return '非法时间格式'; if(data.info.startAt && data.info.endAt && (data.info.startAt > data.info.endAt)) return '结束时间不能小于开始时间'">
                            >
                            <template slot="append">时:分:秒</template>
                        </el-input>
                    </kv>

                    <kv label="心跳休息时间">
                        <el-input v-model="info.heartbeatTime" chk="*">
                            <template slot="append">秒</template>
                        </el-input>
                    </kv>
                    <kv label="监控间隔时间">
                        <el-input v-model="info.infoTime" chk="*">
                            <template slot="append">秒</template>
                        </el-input>
                    </kv>

                    <kv label="文件传输休息时间">
                        <el-input v-model="info.sleepTime" chk="*">
                            <template slot="append">秒</template>
                        </el-input>
                    </kv>
                    <kv label="监控客户端文件夹">
                        <div>
                            <el-input v-model="info.workDir" chk="*"/>
                            <div>不建议设置为一级目录。检查启动客户端程序的权限是否有对该文件夹父文件夹的读写权限</div>
                        </div>
                    </kv>
                    <kv label="服务器存储文件夹">
                        <el-input v-model="info.serverDir" chk="*"/>
                    </kv>
                    <kv label="服务器保存压缩文件文件夹">
                        <div>
                            <el-input v-model="info.serverZipDir"/>
                            <div>如果为空，压缩文件不会在服务器端保存</div>
                        </div>
                    </kv>
                </el-card>
            </el-col>
            <el-col>
                <el-card shadow="always">
                    <div slot="header">
                        <span>参数设置</span>
                    </div>

                    <kv label="是否禁用">
                        <selector type="radio" chk="*" v-model="info.isDisabled" :clearable="false"
                                  :data="{true:'禁用',false:'启用'}" :readOnly="!jv.main.user.isAdmin"/>
                    </kv>
                    <kv label="验证文件完整性">
                        <selector type="radio" chk="*" v-model="info.needCheckFile" :clearable="false"
                                  :data="{true:'验证',false:'不验证'}" :readOnly="!jv.main.user.isAdmin"/>
                    </kv>
                    <kv label="启用压缩">
                        <selector type="radio" chk="*" v-model="info.needZip" :clearable="false"
                                  :data="{true:'启用',false:'禁用'}" :readOnly="!jv.main.user.isAdmin"/>
                    </kv>
                    <kv label="压缩文件是否动态密码">
                        <selector type="radio" chk="*" v-model="info.zipFileDynamicPassword" :clearable="false"
                                  :data="{true:'动态密码',false:'固定密码'}" :readOnly="!jv.main.user.isAdmin"/>
                    </kv>
                    <kv label="压缩文件固定密码" v-if="!info.zipFileDynamicPassword">
                        <div>
                            <el-input v-model="info.zipFilePassword"></el-input>
                        </div>
                    </kv>
                    <!--                    <kv label="服务器保存压缩文件">-->
                    <!--                        <selector type="radio" chk="*" v-model="info.   retainZip" :clearable="false"-->
                    <!--                                  :data="{true:'启用',false:'禁用'}"/>-->
                    <!--                    </kv>-->
                    <el-divider></el-divider>
                    <div v-if="id">
                        <div>分类存储规则:</div>
                        <folder_rule_list :id="id"></folder_rule_list>
                    </div>
                </el-card>
            </el-col>
        </el-row>


        <el-card v-if="id" style="margin-top:30px;">
            <div slot="header">
                <span>运行状态</span>
            </div>
            <space_list :id="id"></space_list>
        </el-card>
    </div>
</template>
<style scoped>
    >>> .kv .k {
        flex: 4 !important;
        min-width: auto !important;
    }
</style>
<script>
    /**
     * Created by CodeGenerator
     */
    import folder_rule_list from "../site_com/folder_rule_list"
    import space_list from "../site_com/space_list"

    export default {
        components: {
            folder_rule_list,
            space_list
        },
        props: {
            id: {type: String, default: ""}
        },
        data() {
            return {
                sizeInfo: 10,
                info: {
                    heartbeatTime: 10,
                    infoTime: 10,
                    sleepTime: 60,
                    workDir: "/data",
                    serverDir: "/data/01",
                    isDisabled: false,
                    needCheckFile: false,
                    needZip: false,
                    retainZip: false,
                }, //子对象需要声明。

            }
        },
        computed: {
            action() {
                return this.id ? "edit" : "add";
            },
            action_name() {
                return {add: "添加", edit: "修改"}[this.action]
            }
        },
        mounted() {
            this.loadData();
        },
        methods: {
            gen_secret() {
                this.$http.post("/site/b-site-info/secret").then(res => {
                    this.info.secret = res.data.data;
                });
            },
            loadData() {
                if (this.id) {
                    this.$http.post("/site/b-site-info/detail/" + this.id).then(res => {
                        this.info = res.data.data;

                        if (!jv.main.user.isAdmin) {
                            this.$nextTick(() => {
                                this.$refs.card.$el.nonEdit();
                            });
                        }
                    });
                }
            },
            save_click() {
                //校验
                if (this.$refs.card.chk() == false) {
                    return;
                }

                this.$http.post("/site/b-site-info/save", this.info).then(res => {
                    jv.info(this.action_name + " 成功");
                    if (this.action == "add") {
                        this.$router.push("/site/b-site-info/edit/" + res.data.data)
                    } else if (this.action == "edit") {
                        this.$router.push("/site/b-site-info/list")
                    }
                })
            },
            // edit_click(row) {
            //     //记录上次点击行
            //     var rowId = row.id;
            //     this.$refs.list.setLastRow(row);
            //     this.$router.push("/site/b_site_folder/edit/" + rowId)
            // },
            // delete_click(row) {
            //     var rowId = row.id;
            //     var rowName = row.name;
            //     jv.confirm('确认删除 ' + rowName + ' 吗？').then(res => {
            //         this.$http.post("/site/b_site_folder/delete/" + rowId).then(res => {
            //             this.loadData();
            //         })
            //     });
            // },
        }
    }
</script>
